/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package content;

import entity.*;
import java.util.*;
import javax.ejb.FinderException;
import javax.naming.*;
import transfer.Group;
import transfer.User;

/**
 *
 * @author Sergey
 */
public class GroupContent {

    public static ArrayList getAllGroups() {
        try {
            Context context = new InitialContext();
            GroupsLocalHome grouphome = (GroupsLocalHome) context.lookup("java:comp/env/Groups");
            Collection collection = grouphome.findAllGroups();
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((GroupsLocal) iterator.next()).getGroup());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Group getGroup(Integer id) {
        try {
            Context context = new InitialContext();
            GroupsLocalHome grouphome = (GroupsLocalHome) context.lookup("java:comp/env/Groups");
            return ((GroupsLocal) grouphome.findByPrimaryKey(id)).getGroup();
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }
     public static ArrayList getByUser(User user) {
        try {
            Context context = new InitialContext();
            GroupsLocalHome grouphome = (GroupsLocalHome) context.lookup("java:comp/env/Groups");
            Collection collection = grouphome.findByUser(user);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((GroupsLocal) iterator.next()).getGroup());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }   
}
